package com.cwj.thread.cancelthread;

public class VolatileFlag {
    public static void main(String[] args) {
        Runner runner = new Runner();
        Thread thread = new Thread(runner, "one Thread");
        thread.start();

        try {
            Thread.sleep(1_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(System.currentTimeMillis());
        runner.cancel();

    }

    private static class Runner implements Runnable {
        private volatile boolean flag = false;
        @Override
        public void run() {
            while (!flag) {
                System.out.println(Thread.currentThread().getName() + " running");
            }
            System.out.println(System.currentTimeMillis());
            System.out.println("end");
        }

        public void cancel(){
            flag = true;
        }
    }
}